<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <div class="top-box">
      <div class="top-item"><img src="../../assets/images/zongcaigoudingdan.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.orderNum}}</span><br>
            <span class="fontText">总采购订单(单)</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/zongcaigoushuliang.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.orderCount}}</span><br>
            <span class="fontText">总采购数量(个)</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/zaitugeshu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.roadIng}}</span><br>
            <span class="fontText">在途个数(个)</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/daifadancaigoushu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.confrimCount}}</span><br>
            <span class="fontText">待发单采购数(单)</span>
          </p>
        </div>
      </div>
      <!-- <el-button type="primary" @click="addhouse">新增运营中心</el-button> -->
    </div>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="订单编号"
                    prop="">
        <el-input v-model="searchForm.info"
                  placeholder="请输入订单编号"></el-input>
      </el-form-item>
      <el-form-item label="采购公司">
        <el-select v-model="searchForm.cid"
                   placeholder="请选择采购公司">
          <el-option v-for="(item,index) in comList"
                     :key="index"
                     :label="item.name"
                     :value="item.shopId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.sid"
                   placeholder="请选择状态">
          <el-option label="已取消"
                     value="0"></el-option>
          <el-option label="已完成"
                     value="1"></el-option>
          <el-option label="待发单"
                     value="2"></el-option>
          <el-option label="已发单待发货"
                     value="3"></el-option>
          <el-option label="发货中"
                     value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="大区运营">
        <el-select v-model="searchForm.qid"
                   placeholder="请选择大区运营">
          <el-option v-for="(item,index) in opList"
                     :key="index"
                     :label="item.opName"
                     :value="item.opId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div>
      <p>总订单数&nbsp;&nbsp;共计<span>{{searchForm.totalCount}}条</span></p>
    </div>
    <div class="data-container scroll-table">
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="55">
        </el-table-column>
        <el-table-column prop="opOrderNo"
                         label="采购单编号">
        </el-table-column>
        <el-table-column prop="shopName"
                         label="采购公司">
        </el-table-column>
        <el-table-column prop="cityName"
                         label="所在地址">
        </el-table-column>
        <el-table-column prop="address"
                         label="详细地址">
        </el-table-column>
        <el-table-column prop="name"
                         label="客户姓名">
        </el-table-column>
        <el-table-column prop="phone"
                         label="联系电话">
        </el-table-column>
        <el-table-column prop="brandName"
                         label="采购品牌">
        </el-table-column>
        <!-- <el-table-column prop="cityId"
                         label="规格">
        </el-table-column> -->
        <el-table-column prop="count"
                         label="数量">
        </el-table-column>
        <el-table-column prop="allMoney"
                         label="订单总金额(元)">
          <template slot-scope="scope">
            <p v-if="scope.row.allMoney==null||scope.row.allMoney==undefined">0</p>
            <p v-else>{{scope.row.allMoney/100}}</p>
          </template>
        </el-table-column>
        <el-table-column prop=""
                         label="收取订单金额(员)">
          <template slot-scope="scope">
            <p v-if="scope.row.skuCount==null||scope.row.skuCount==undefined">0</p>
            <p v-else>{{scope.row.skuCount/100}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="opName"
                         label="接单运营名称">
        </el-table-column>
        <el-table-column prop="contactsName"
                         label="运营联系人">
        </el-table-column>
        <el-table-column prop="contactsPhone"
                         label="运营联系电话">
        </el-table-column>
        <el-table-column prop="logisticsName"
                         label="物流名称">
        </el-table-column>
        <el-table-column prop="logisticsNo"
                         label="物流单号">
        </el-table-column>
        <el-table-column prop="addtime"
                         label="创建时间">
        </el-table-column>
        <el-table-column prop="pretime"
                         label="订单收取时间">
        </el-table-column>
        
        <el-table-column prop="bank"
                         label="收款开户行">
        </el-table-column>
        <el-table-column prop="bankNo"
                         label="收款账号">
        </el-table-column>
        <el-table-column prop="isInvoice"
                         label="是否开票">
          <template slot-scope="scope">
            <div v-if="scope.row.isInvoice==1">是</div>
            <div v-if="scope.row.isInvoice==0">否</div>
          </template>
        </el-table-column>
        <el-table-column prop="balance"
                         label="尾款金额">
             <template slot-scope="scope">
            <p v-if="scope.row.balance==null||scope.row.balance==undefined">0</p>
            <p v-else>{{scope.row.balance/100}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="endtime"
                         label="签收时间">
        </el-table-column>
        <el-table-column prop="state"
                         label="状态">
          <template slot-scope="scope">

            <div v-if="scope.row.state==0">已取消</div>
            <div v-if="scope.row.state==1">已完成</div>
            <div v-if="scope.row.state==2">待发单</div>
            <div v-if="scope.row.state==3">已发单待发货</div>
            <div v-if="scope.row.state==4">发货中</div>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="mgt-10 pd-0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="searchForm.current"
                     :page-sizes="[10, 20, 30, 50]"
                     :page-size="searchForm.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="searchForm.totalCount">
      </el-pagination>
    </div>
    <!--新增修改界面-->
    <el-dialog title="新增采购单"
               :visible.sync="AddFormVisible">
      <el-form :model="addForm"
               ref="addForm"
               :rules="rules">
        <el-form-item label="采购公司选择"
                      :label-width="formLabelWidth"
                      prop="shopId">
          <el-select v-model="addForm.shopId"
                     placeholder="请选择采购公司">
            <el-option v-for="(item,index) in comList"
                       :key="index"
                       :value="item.shopId"
                       :label="item.shopName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌选择"
                      :label-width="formLabelWidth"
                      prop="skuId">
          <el-select v-model="addForm.skuId"
                     placeholder="请选择采购品牌">
            <el-option v-for="(item,index) in brandList"
                       :key="index"
                       :value="item.skuId"
                       :label="item.brandName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量"
                      :label-width="formLabelWidth"
                      prop="count">
          <el-input v-model="addForm.count"
                    autocomplete="off" type="number"></el-input>
        </el-form-item>
        <!-- <el-form-item label="单价(元)"
                      :label-width="formLabelWidth"
                      prop="price" type="number">
          <el-input v-model="addForm.price"
                    autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="总订单金额(元)"
                      :label-width="formLabelWidth"
                      prop="allMoney" type="number">
          <el-input v-model="addForm.allMoney"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收款账户"
                      :label-width="formLabelWidth"
                      prop="companyName">
               
          <el-select v-model="addForm.payId"
                     placeholder="请选择收款账户">
            <el-option v-for="(item,index) in accountList"
                       :key="index"
                       :value="item.payId"
                       :label="item.companyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开票"
                      :label-width="formLabelWidth"
                      prop="isInvoice">
          <el-radio-group v-model="addForm.isInvoice">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="接单运营"
                      :label-width="formLabelWidth"
                      prop="opId">
          <el-select v-model="addForm.opId"
                     placeholder="请选择接单运营">
            <el-option v-for="(item,index) in opList"
                       :key="index"
                       :value="item.opId"
                       :label="item.opName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="AddFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="sendHouse('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import btns from '@/components/btns.vue'
import ExportExcel from "../../utils/ExportExcel.js";
import breadcrumbs from "@/components/breadcrumbs.vue";
import {unitPriceValidate,integerNumValidate} from "../../utils/tool"

import {
  format
} from "../../utils/tool"
export default {
  components: {
    btns,
    ExportExcel,
    breadcrumbs,
  },
  data () {
    return {
       cityData: [],
      houseArr: [],
      formLabelWidth: "120px",
      AddFormVisible: false,
      addForm: {
        houseIds: []
      },
      opList: [],
      yearData: "",
      topData: {},
      btnForm: {},
      searchForm: {
        name: ''
      },
      comList: [],
      accountList: [],
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      brandList: [],
      cityData: [],
      rules: {
        "btnIcon": [],
        "btnName": [{
          required: true,
          message: '请输入城市名称',
          trigger: 'blur'
        }],
        "brandName": [{
          required: true,
          message: '请选择品牌',
          trigger: 'blur'
        }],
        "btnCode": [{
          required: true,
          message: '请选择状态',
          trigger: 'blur'
        }],
        "sort": [{
          required: true,
          message: '排序',
          trigger: 'blur'
        }],
        "shopId": [{
          required: true,
          message: '请选择采购公司',
          trigger: 'change'
        }],
        
          "allMoney": [{
          required: true,
          message: '请输入总订单金额',
          trigger: 'change'
        },
        { validator: unitPriceValidate, trigger: 'blur' }],
        "brandName": [{
          required: true,
          message: '请输入规格类目',
          trigger: 'change'
        }],
        "count": [{
          required: true,
          message: '请输入采购数量',
          trigger: 'blur'
        },
        { validator: integerNumValidate, trigger: 'blur' }
        ],
        "allMoney": [{
          required: true,
          message: '请输入订单总价格',
          trigger: 'blur'
        },
         { validator: unitPriceValidate, trigger: 'blur' }
        ],
        "payId": [{
          required: true,
          message: '请输入收款账户',
          trigger: 'blur'
        }],
        "isInvoice": [{
          required: true,
          message: '请选择是否开票',
          trigger: 'blur'
        }],
        "account": [{
          required: true,
          message: '请输入收款账号',
          trigger: 'change'
        }],
        "opId": [{
          required: true,
          message: '请选择接单运营',
          trigger: 'change'
        }],
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    this.getCityList();
    this.getOperateNum();
    this.getTableData(1, 10);
    // this.getopList();
    this.getINfoList();
    
  },
  methods: {
    getCityList () {
      this.$api.city.getAllList().then(d => {
        if (d.code == 200) {
          this.cityData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    getINfoList () {
      this.$api.operate.getAdminOpShopOrderForm().then(res => {
        if (res.code == 200) {
          this.accountList = res.data.payList;
          this.brandList = res.data.brandSkuList;
          this.opList = res.data.operateList;
          this.comList = res.data.shopList;
          console.log(res.data.payList)
        }
      })
    },
    // getBrandList () {
    //   this.$api.brand.getAdminBrandList().then(res => {
    //     if (res.code == 200) {
    //       this.brandList = res.data;
    //     }
    //   })
    // },
    // getCityList () {
    //   this.$api.city.getList({
    //     current: 1,
    //     size: 10000
    //   }).then(d => {
    //     if (d.code == 200) {
    //       this.cityData = d.data;
    //     } else {
    //       this.$message.error(d.msg);
    //     }
    //   }).catch((e) => {
    //     console.log(e);
    //   })
    // },
    // getopList () {
    //   this.$api.operate.getAdminOpList({
    //     current: 1,
    //     size: 10000,
    //   }).then(res => {
    //     if (res.code == 200) {
    //       this.opList = res.data;
    //       console.log(this.opList, 'oplist')
    //     }
    //   })
    // },
    // getcomList () {
    //   this.$api.shopList.getAdminShopList({
    //     current: 1,
    //     size: 10000
    //   }).then(res => {
    //     if (res.code == 200) {
    //       let result = res.data;
    //       console.log(result, 'result')
    //       this.comList = result
    //     }
    //   })
    // },
    Completed () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      let demo = this.tableSelection[0]
      this.$confirm('此操作将会对该数据进行已完成操作, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.operate.endOpShopOrder({
          opShopOrderId: demo.opShopOrderId
        }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size);
            this.$message.success('已完成成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });

    },
    cancel () {
      console.log(222)
    },
    gototogetHouseMent (row) {
      this.$router.push({
        path: "/shop/shopDetail",
        query: {
          shopId: row.shopId
        }
      })
    },
    getHouseArr () {
      this.$api.operate.getNotCityHouseList({
        cityId: 1,
      }).then(res => {
        if (res.code == 200) {
          this.houseArr = res.data;
        }
      })
    },
    getHouse () {
      console.log(this.addForm.houseIds)
    },
    getOperateNum () {
      this.$api.opOrder.getAdminOpShopOrderCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data;
        }
      })
    },
    sendHouse (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.funTit == "add") {
            console.log(this.addForm)
            this.$api.operate.addOpShopOrder({
              count: this.addForm.count,
              isInvoice: this.addForm.isInvoice,
              opId: this.addForm.opId,
              payId: this.addForm.payId,
              price: parseInt(this.addForm.allMoney*100),
              shopId: this.addForm.shopId,
              skuId: this.addForm.skuId
            }).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "新增采购公司成功"
                })
                this.addForm={
                 
                  count:'',
                  isInvoice:'',
                  opId:'',
                  price:'',
                  payId:'',
                  allMoney:'',
                  payId:'',
                  shopId:'',
                  skuId:'',


                }
                this.getTableData(this.searchForm.current, this.searchForm.size);
                this.AddFormVisible = false;
              }
              else{
                this.$message.error(d.msg);
              }
            })

            /* 编辑采购公司 */
          } else {
            this.$api.operate.editOpShopOrder({
              count: this.addForm.count,
              isInvoice: this.addForm.isInvoice,
              opId: this.addForm.opId,
              payId: this.addForm.payId,
              price: parseInt(this.addForm.allMoney)*100,
              shopId: this.addForm.shopId,
              skuId: this.addForm.skuId,
              orderId: this.addForm.opShopOrderId
            }).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改采购公司成功"
                })
                this.getTableData(this.searchForm.current, this.searchForm.size);
                this.AddFormVisible = false;
              }
              else{
                console.log("错误")
                this.$message.error(d.msg)
              }
            })
          }

        }
      })
    },
    addhouse () {
      this.AddFormVisible = true
    },
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableData(this.searchForm.current, v);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableData(v, this.searchForm.size);
    },
    getTableData (curr, size, info, cid, sid, qid) {
      this.$api.opOrder.getAdminOpShopOrderList({
        current: curr,
        size: size,
        opId: qid,
        shopId: cid,
        orderNo: info,
        state: sid
      }).then(res => {
        if (res.code == 200) {
          this.tableData = res.data;
          // console.log(this.tableData)



           this.tableData.forEach((item) => {
            this.cityData.forEach((op) => {
              console.log(item, op)
              if (item.cityId == op.cityid) {
                item.cityName = op.cityname
              }
            })
          })
        //  console.log("this.cityname="+this.cityName)
          this.searchForm.totalCount = res.totalCount;
          this.searchForm.size = res.pageSize
        }
      })
    },
    chageYearData () {
      this.$api.recycleBin.getAdminBackStopCount({
        year: this.yearData
      }).then(res => {
        if (res.code == 200) {
          this.topData = res.data
          this.searchForm.totalCount = res.totalCount;
          this.searchForm.size = res.pageSize
        }
      })
    },
    getYearData () {
      this.$api.recycleBin.getAdminBackStopCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data
        }
      })
    },
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.getTableData(this.searchForm.current, this.searchForm.size);
    },
    search () {
      this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.cid, this.searchForm.sid, this.searchForm.qid);
    },
    btnlist () {
      //获取接口
      this.$api.city.getList({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {
      this.AddFormVisible = true;
    },
    update () {
      console.log("进入编辑")
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
    

       this.addForm = this.tableSelection[0];

    
      this.addForm.allMoney=this.tableSelection[0].allMoney/100;
      console.log(this.addForm)
      this.AddFormVisible = true;
      // this.comList.forEach((item) => {
       
      //   if (this.addForm.shopId == item.shopId) {
      //     this.addForm.shopName = item.shopName
      //   }
        
      // })
      // this.brandList.forEach((item) => {
      //   if (this.addForm.skuId == item.skuId) {
      //     this.addForm.brandName = item.brandName
      //   }
        
      //  })
      //  console.log("this.addForm.cid++++++++++++++++++++++"+this.addForm.cid)
      //   console.log("this.addForm.bid---------------------"+this.addForm.bid)

       console.log("this.addForm.shopName++++++++++++++++++++++"+this.addForm.shopName)

       console.log("this.addForm.brandName---------------------"+this.addForm.brandName)

       console.log("this.addForm.num---------------------"+this.tableSelection[0].num)
    
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.shopList.updateStatus({
          shopId: demo.shopId,
          status: 1
        }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size);
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 0) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.shopList.updateStatus({
          shopId: demo.shopId,
          status: 2
        }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
      export(){
       console.log("导出")
       this.$api.outorder
        .getcaigoudaneOutOrderExcel({ 

        })
        .then((d) => {
          let filename = "采购单明细表.xlsx";
          ExportExcel(d, filename); 
        })
        .catch((e) => {
          console.log(e);
        });

    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.findIndex(c => c.btnCode == this.btnForm.btnCode) >= 0) {
            this.$message.error('code不能重复');
            return
          }
          if (this.funTit == 'add') {
            this.$api.system.btnadd(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.system.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.fontText {
  font-size: 20px;
}

.fontTop {
  font-size: 40px;
}

.top-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.top-item {
  border: 1px gray solid;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  padding: 20px;

  .top-icon {
    width: 35%;
    height: 35%;

  }

  .right-item-box {
    text-align: center;
    margin-left: 20px;
  }
}
</style>
